//
//  AddPictureViewController.m
//  QT
//
//  Created by 甘世清 on 2025/1/6.
//

#import "AddPictureViewController.h"
#import "MapAddressViewController.h"
@interface AddPictureViewController ()
@property (nonatomic , strong) SQCustomButton * imageBtn;
@property (nonatomic , strong) FSTextView * fsTextView;
@property (nonatomic , strong) UILabel * addressLabel;
@property (nonatomic , strong) NSMutableDictionary * data;
@end

@implementation AddPictureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = SQColorF5F5F5;
    self.zx_navTitle = SQNSLocalString(@"发布");
    WS(weakSelf);
    [self zx_setRightBtnWithText:SQNSLocalString(@"提交") clickedBlock:^(ZXNavItemBtn * _Nonnull btn) {
        [weakSelf SQ_tijiaoButtonClick];
    }];
}

- (void)SQ_UIViewForSelfView{
    UIView * bgView = [[UIView alloc]init];
    bgView .backgroundColor = UIColor.whiteColor;
    bgView.layer.masksToBounds = YES;
    bgView.layer.cornerRadius = 20;
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-30);
        make.height.mas_equalTo(140);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(10);
    }];
    [self.view addSubview:self.fsTextView];
    [self.fsTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-40);
        make.height.mas_equalTo(120);
        make.top.equalTo(self.zx_navBar.mas_bottom).offset(20);
    }];
    
    UIView * bgView2 = [[UIView alloc]init];
    bgView2 .backgroundColor = UIColor.whiteColor;
    bgView2.layer.masksToBounds = YES;
    bgView2.layer.cornerRadius = 6;
    [self.view addSubview:bgView2];
    [bgView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-30);
        make.height.mas_equalTo(48);
        make.top.equalTo(bgView.mas_bottom).offset(10);
    }];
    UIImageView * rightImage = [[UIImageView alloc]init];
    rightImage.image = SQGetImage(@"youjiantou");
    [bgView2 addSubview:rightImage];
    [rightImage mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(bgView2);
            make.size.mas_equalTo(16);
            make.right.equalTo(bgView2.mas_right).offset(-10);
    }];
    
    [bgView2 addSubview:self.addressLabel];
    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(bgView2).offset(10);
            make.top.bottom.equalTo(bgView2);
            make.width.mas_equalTo(SCREEN_WIDETH-70);
    }];
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = UIColor.clearColor;
    [btn addTarget:self action:@selector(SQ_addressButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [bgView2 addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(bgView2);
    }];
    [self.view addSubview:self.imageBtn];
    [self.imageBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.width.mas_equalTo(SCREEN_WIDETH-30);
        make.bottom.equalTo(self.view.mas_bottom).offset(-FootHeight-30);
        make.top.equalTo(bgView2.mas_bottom).offset(30);
    }];
    WS(weakSelf);
    [self.imageBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_HeaderViewqButtonClick];
    }];
}
-(void)SQ_tijiaoButtonClick{
    if (![self.fsTextView.formatText isNotEmpty]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"描述不能为空!")];
        return;
    }
    if ([self.addressLabel.text isEqualToString:SQNSLocalString(@"添加位置(将根据位置优先推荐附近人查看)")]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"地址不能为空!")];
        return;
    }
    if ([self.addressLabel.text isEqualToString:SQNSLocalString(@"添加位置(将根据位置优先推荐附近人查看)")]) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"地址不能为空!")];
        return;
    }
    if (self.imageBtn.SQBackimageView.image ==nil) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"请设置相易图片!")];
        return;
    }
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    WS(weakSelf);
    [HttpTool SQ_Image:self.imageBtn.SQBackimageView.image successBlock:^(BaseModel * _Nonnull httpModel) {
        NSString * imageurl = [NSString stringWithFormat:@"%@",httpModel.data];
        [weakSelf SQ_updateImage:imageurl];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:SQNSLocalString(@"网络异常请重试")];
    }];
}
-(void)SQ_updateImage:(NSString*)headerImage{
    [self.data setObject:headerImage forKey:@"imgs"];
    [self.data setObject:self.fsTextView.formatText forKey:@"content"];
    [YJProgressHUD SQ_showMsgWithoutView:@""];
    [HttpTool post:@"dynamic/save" paramDic:self.data successBlock:^(BaseModel * _Nonnull httpModel) {
        [YJProgressHUD  showMsgWithoutView:SQNSLocalString(@"发布成功")];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self SQ_popViewVC];
        });
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}

-(void)SQ_addressButtonClick{
    [self.view endEditing:YES];
    MapAddressViewController * vc = [[MapAddressViewController alloc]init];
    [self SQ_pushViewControllerFull:vc];
    WS(weakSelf);
    [vc setAddressButtonBlock:^(NSMutableDictionary * _Nonnull addressDic) {
        weakSelf.addressLabel.text = [addressDic objectForKey:@"address"];
        weakSelf.data = [[NSMutableDictionary alloc]initWithDictionary:addressDic];
        weakSelf.addressLabel.textColor = SQColor333333;
    }];
}
-(void)SQ_HeaderViewqButtonClick{
    WS(weakSelf);
    [self.view endEditing:YES];
    ImagePopView * vc = [[ImagePopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDETH, 200+FootHeight)];
    LSTPopView * pop = [LSTPopView initWithCustomView:vc
                                           parentView:nil
                                             popStyle:LSTPopStyleSmoothFromBottom dismissStyle:LSTDismissStyleSmoothToBottom];
    pop.hemStyle = LSTHemStyleBottom;
    [pop pop];
    LSTPopViewWK(pop)
    [pop setBgClickBlock:^{
        [wk_pop dismiss];
    }];
    [vc setAddImageArrayBlock:^(NSArray<UIImage *> * _Nonnull imageArray) {
        weakSelf.imageBtn.SQBackimageView.image = imageArray.firstObject;
        weakSelf.imageBtn.imageView.hidden = YES;
        [wk_pop dismiss];
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (UILabel *)addressLabel{
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc]init];
        _addressLabel.textColor = SQColor999999;
        _addressLabel.font = FONT_MEDIUM(14);
        _addressLabel.text = SQNSLocalString(@"添加位置(将根据位置优先推荐附近人查看)");
    }
    return _addressLabel;
}
- (SQCustomButton *)imageBtn{
    if (!_imageBtn) {
        _imageBtn = [[SQCustomButton alloc]initType:SQCustomButtonLeftImageType imageSize:CGSizeMake(60, 60) midmargin:0];
        _imageBtn.image = SQGetImage(@"xiangji");
        _imageBtn.SQBackimageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageBtn.clipsToBounds = YES;
        _imageBtn.layer.cornerRadius = 30;
        _imageBtn.layer.borderColor = SQColor333333.CGColor;
        _imageBtn.layer.borderWidth = 0.5;
        _imageBtn.backgroundColor = UIColor.whiteColor;
    }
    return _imageBtn;
}

-(FSTextView*)fsTextView{
    if (!_fsTextView) {
        _fsTextView = [FSTextView textView];
        _fsTextView.font = FONT_MEDIUM(14);
        _fsTextView.textColor = SQColor333333;
        _fsTextView.placeholder =SQNSLocalString(@"简单的描述一下相易的要求吧～");
        _fsTextView.placeholderColor = SQColor999999;
        _fsTextView.backgroundColor = UIColor.clearColor;
        _fsTextView.maxLength = 200;
       
    }
    return _fsTextView;
}

@end
